﻿using System.Data;
using BusinessLogic.BusinessRules;
using DataAccess;
using Model.Data;

namespace BusinessLogic.ClassFactory
{
    /// <summary>
    /// ArticleFactory 的摘要说明。
    /// </summary>
    public class ArticleFactory : BaseFactory
    {
        public ArticleFactory()
            : base()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public ArticleFactory(DBManager l_DBManager)
            : base(l_DBManager)
        {
        }

        public Article NewInstance()
        {
            ArticleAccess l_Access = new ArticleAccess(m_Manager);
            Article objArticle = new Article(this.m_Manager, l_Access);
            objArticle.NewDataSet();
            objArticle.SelectRow(0);
            return objArticle;
        }

        public int DeleteArticle(int intID)
        {
            ArticleAccess l_Access = new ArticleAccess(m_Manager);
            return l_Access.Delete(intID);
        }

        public int DeleteArticleWhere(string where)
        {
            ArticleAccess l_Access = new ArticleAccess(m_Manager);
            return l_Access.DeleteWhere(where);
        }

        public int UpdateSort(int intSectionID)
        {
            ArticleAccess l_Access = new ArticleAccess(m_Manager);
            return l_Access.UpdateSort(intSectionID);
        }

        public int QueryByCompletionSql(string Sql)
        {
            ArticleAccess l_Access = new ArticleAccess(m_Manager);
            Article objArticle = new Article(this.m_Manager, l_Access);
            DataSet l_ds = new ArticleData();
            if (l_Access.QueryByQuerySql(ref l_ds, Sql) < 1)
            {
                return 0;
            }
            objArticle.SetDataSet(l_ds);
            objArticle.SelectRow(0);
            return objArticle.Data.Article.Rows.Count;
        }

        public Article CreatorBySql(string Sql)
        {
            ArticleAccess l_Access = new ArticleAccess(m_Manager);
            Article objArticle = new Article(this.m_Manager, l_Access);
            DataSet l_ds = new ArticleData();
            if (l_Access.QueryBySql(ref l_ds, Sql) < 1)
            {
                return null;
            }
            objArticle.SetDataSet(l_ds);
            objArticle.SelectRow(0);
            return objArticle;
        }

        public DataSet CreatorBySqlDataSet(string Sql)
        {
            ArticleAccess l_Access = new ArticleAccess(m_Manager);
            DataSet objDataSet = new DataSet();
            l_Access.QueryByQuerySql(ref objDataSet, Sql);
            return objDataSet;
        }

        public Article CreatorByFullSql(string Sql)
        {
            ArticleAccess l_Access = new ArticleAccess(m_Manager);
            Article objArticle = new Article(this.m_Manager, l_Access);
            DataSet l_ds = new ArticleData();
            if (l_Access.QueryByQuerySql(ref l_ds, Sql) < 1)
            {
                return null;
            }
            objArticle.SetDataSet(l_ds);
            objArticle.SelectRow(0);
            return objArticle;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="intArticleID"></param>
        /// <returns></returns>
        public Article Creator(int intArticleID)
        {
            ArticleAccess l_Access = new ArticleAccess(m_Manager);
            Article objArticle = new Article(this.m_Manager, l_Access);
            DataSet l_ds = new ArticleData();
            if (l_Access.QueryByID(ref l_ds, intArticleID) < 1)
            {
                return null;
            }
            objArticle.SetDataSet(l_ds);
            objArticle.SelectRow(0);
            return objArticle;
        }

        public Article Creator()
        {
            ArticleAccess l_Access = new ArticleAccess(m_Manager);
            Article objArticle = new Article(this.m_Manager, l_Access);
            DataSet l_ds = new ArticleData();
            if (l_Access.QueryAll(ref l_ds) < 1)
            {
                return null;
            }
            objArticle.SetDataSet(l_ds);
            objArticle.SelectRow(0);
            return objArticle;
        }
    }
}